package mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import modelo.Rodillo;
import controlador.Tragamonedas;

public class AdmPersistenciaRodillo extends AdmPersistencia<Rodillo> {

	private static AdmPersistenciaRodillo instancia;

	private AdmPersistenciaRodillo() {

	}

	public static AdmPersistenciaRodillo getInstancia() {
		if (instancia == null)
			instancia = new AdmPersistenciaRodillo();
		return instancia;
	}

	@Override
	public void insertar(Rodillo a) {
		try {
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con
					.prepareStatement("insert into dbo.Rodillo values (?,?,?)");
			// agregar campos
			s.setInt(1, (int) a.getId());
			s.setFloat(2, (int) a.getIdTragamonedas());
			s.setFloat(3, (int) a.getPosicion());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	@Override
	public void actualizar(Rodillo a) {
		try {
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("update dbo.Rodillo " +
					"set id_rodillo = ?," +
					"id_tragamonedas =?," +
					"posicion =? where id_rodillo = ?");
			//agregar campos
			s.setFloat(1, a.getId());
			s.setFloat(2,a.getIdTragamonedas());
			s.setFloat(3, a.getPosicion());
			s.setFloat(4, (int) a.getId());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	@Override
	public void borrar(Rodillo a) {
		try {
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("delete from dbo.Rodillo where id_rodillo = ?");
			s.setInt(1, (int) a.getId());
			s.execute();
			PoolConnection.getPoolConnection().realeaseConnection(con);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	@Override
	public ArrayList<Rodillo> seleccionar(long id) {
		ArrayList<Rodillo> rodillos;
		try {
			rodillos = new ArrayList<Rodillo>();
			Rodillo a = null;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select id_rodillo, posicion from dbo.Rodillo where id_tragamonedas = ?");
			s.setLong(1,id);
			ResultSet result = s.executeQuery();
			while (result.next()) {
				int idRodillo = (int) result.getFloat(1);
				int idTragamonedas = (int) (Tragamonedas.getInstancia().getId());
				int posicion = (int) result.getFloat(2);

				a = new Rodillo(idRodillo, idTragamonedas, posicion);

				rodillos.add(a);
			}

			PoolConnection.getPoolConnection().realeaseConnection(con);
			return rodillos;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	@Override
	public Rodillo buscar(long id) {
		try {
			Rodillo a = null;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select id_rodillo, id_tragamonedas, posicion from dbo.Rodillo where id_rodillo = ?");
			s.setLong(1,id);
			ResultSet result = s.executeQuery();
			while (result.next()) {
				int idRodillo = (int) result.getFloat(1);
				int idTragamonedas = (int) result.getFloat(2);
				int posicion = (int) result.getFloat(3);

				a = new Rodillo(idRodillo, idTragamonedas, posicion);
			}

			PoolConnection.getPoolConnection().realeaseConnection(con);
			return a;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

}
